package com.cupk.carManageMaster.common.mapper;


import com.cupk.carManageMaster.common.domain.DispatchCar;
import com.cupk.carManageMaster.common.domain.Driver;
import com.cupk.carManageMaster.common.domain.MessageHelp;
import com.cupk.carManageMaster.common.domain.User;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;

/**
 * 名称DriverMapper
 * 描述
 *
 * @version 1.0
 * @author:wanghao
 * @datatime:2023,06,03,19,14
 */
@Repository
@Mapper
public interface DriverMapper {
    @Select("select id,job_number,employee_name,entry_time,age,photo_url,user_password,privilege_level,id_number,create_time,update_time,is_delete,phone_number from job_user where job_number = #{jobNumber}")
    User getDriverInfoByNum(String jobNumber);

    @Select("select id,mileage,driver_id,car_id,schedule_start_time,expected_schedule_end_time,message,task_status,schedule_end_time,longitude,latitude,create_time,update_time,is_delete from dispatch_car where driver_id=#{id} and task_status=#{taskStatus}")
    List<DispatchCar> getJobsById(@Param("id") Integer id, @Param("taskStatus") Integer status);

    @Update("update dispatch_car set schedule_end_time=#{scheduleEndTime} , task_status=#{taskStatus},update_time=NOW() where id=#{id}")
    void changeWork(@Param("id") Integer id, @Param("taskStatus") Integer status, @Param("scheduleEndTime") Date scheduleEndTime);

    @Select("select id,driving_experience,type_car_id,create_time,update_time,is_delete,task_status from drive_user where user_id = #{user_id}")
    Driver findDriverByUserId(Integer userId);

    @Select("select motorcycle_type from type_car where id = #{id}")
    String findTypeCarById(Integer id);

    @Select("select car_number from message_car where id = #{id}")
    String findCarNumbersById(Integer id);

    @Select("select car_id from dispatch_car where driver_id=#{id}")
    @Result(column = "car_id", property = "carId", jdbcType = JdbcType.BIGINT)
    List<Integer> findCarIdsById(Integer id);

    @Insert("insert into message_help(is_emergent,alert_time,is_handled,help_seeker,create_time,update_time,is_delete) value(#{isEmergent},#{alertTime},1,#{helpSeeker},NOW(),NOW(),0)")
    void insertHelp(Integer isEmergent, Date alertTime, Integer helpSeeker);

    @Select("select mileage from dispatch_car where id=#{id}")
    int findDispatchMileageById(Integer id);

    @Select("select mileage from message_car where id=#{id}")
    int findMessageCarMileageById(Integer id);

    @Update("update message_car set mileage=#{mileage},update_time=NOW() where id = #{id}")
    void updateMessageCarMileageById(@Param("mileage") Integer mileage, @Param("id") Integer id);

    @Select("select car_id from dispatch_car where id=#{id}")
    @Result(column = "car_id", property = "carId", jdbcType = JdbcType.BIGINT)
    int findCarIdById(Integer id);

    @Select("select driver_id from dispatch_car where id = #{id}")
    Integer findDriverIdById(Integer id);

    @Select("select id,mileage,driver_id,car_id,schedule_start_time,expected_schedule_end_time,message,task_status,schedule_end_time,longitude,latitude,create_time,update_time,is_delete from dispatch_car where id=#{id}")
    DispatchCar findDispatchCarById(Integer id);

    @Update("update drive_user set task_status=#{taskStatus} where id = #{id}")
    void updateDriverStatusById(Integer id, Integer taskStatus);

    @Update("update message_car set is_working = #{isWorking} where id = #{id}")
    void updateMessageCarWorkingById(Boolean isWorking, Integer id);
}
